﻿using System;
using System.Collections.Generic;
using System.Net;

namespace Toybox.NicoSharp.Seiga
{
	using Primitives;
	using Utilities;

	public enum SpanType
	{
		/// <summary>
		/// 毎時
		/// </summary>
		hourly,

		/// <summary>
		/// デイリー
		/// </summary>
		daily,

		/// <summary>
		/// 週間
		/// </summary>
		weekly,

		/// <summary>
		/// 月間
		/// </summary>
		monthly,

		/// <summary>
		/// 合計
		/// </summary>
		total,
	}

	public partial class Ranking : AbstractResource
	{

		#region Constructor

		public Ranking(SpanType span, CookieCollection cookies = null)
			: base(Enum.GetName(span.GetType(), span), cookies)
		{
			this.Span = span;
		}

		#endregion Constructor


		#region Public Members

		public SpanType Span
		{
			get;
			protected set;
		}

		#endregion Public Members

		#region Protected Members

		protected virtual string IllustRankingUrl
		{
			get { return NicoUrls.SeigaApi + "illust/ranking/"; }
		}

		protected virtual string ImageUrl
		{
			get { return NicoUrls.SeigaApi + "image/"; }
		}

		protected virtual string CommentAddUrl
		{
			get { return this.ImageUrl + "comment/add"; }
		}

		#endregion Protected Members


		#region Public Methods

		public Data GetData()
		{
			return this.Get<Data>(IllustRankingUrl + "data?span=" + Enum.GetName(this.Span.GetType(), this.Span));
		}


		public CommentAddResult CommentAdd(string ranking_created, string image_id, int frame, string command, string content)
		{
			if (string.IsNullOrEmpty(content))
				throw new ArgumentNullException("content");

			string id = Enum.GetName(this.Span.GetType(), this.Span);
			PostKey key = PostKey.GetKey(id, ActionType.default_flash_request, this.Cookies);

			Dictionary<string, string> dict = new Dictionary<string, string>();
			dict.Add("key_id", id);
			dict.Add("ranking_created", ranking_created);
			dict.Add("image_id", image_id);
			dict.Add("h", key.hash);
			dict.Add("a", key.action);
			dict.Add("t", key.time.ToString());
			dict.Add("frame", frame.ToString());
			dict.Add("command", string.IsNullOrEmpty(command) ? string.Empty : Uri.EscapeDataString(command));
			dict.Add("content", Uri.EscapeDataString(content));

			string data = Serializer.AndEqualPairSerializer(dict).Replace("_", "%5F");
			return this.Post<CommentAddResult>(this.CommentAddUrl, data);
		}


		public override string GetDefaultBGMPath()
		{
			return NicoUrls.LohasTop + "mp3/ranking/bgm.mp3";
		}

		#endregion Public Methods

	}
}
